package com.baizan.daka.ui.activity

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.widget.TextView
import com.baizan.base.BaseActivity
import com.baizan.daka.Constants
import com.baizan.daka.event.TastMrgEvent
import com.baizan.daka.event.UserInfoEvent
import com.baizan.daka.glide.ImageLoadHelp
import com.baizan.daka.help.ImgSelectHelp
import com.baizan.daka.help.ImgSelectHelp.Companion.CODE_CAMERA_REQUEST
import com.baizan.daka.help.ImgSelectHelp.Companion.CODE_GALLERY_REQUEST
import com.baizan.daka.help.ImgSelectHelp.Companion.CODE_RESULT_REQUEST
import com.baizan.daka.mvp.model.bean.UserInfoBean
import com.baizan.daka.ui.contract.UserInfoContract
import com.baizan.daka.ui.presenter.UserInfoPresenter
import com.baizan.daka.showToast
import com.baizan.daka.ui.dialog.ButtomDialog
import com.dianzan.daka.R
import kotlinx.android.synthetic.main.act_user_info.*
import org.greenrobot.eventbus.EventBus
import java.io.File

class UserInfoActivity : BaseActivity<UserInfoPresenter>(), UserInfoContract.View {

    companion object {
        fun start(context: Context, userInfoBean: UserInfoBean) {
            var intent = Intent(context, UserInfoActivity::class.java)
            intent.putExtra("userInfo", userInfoBean)
            context.startActivity(intent)
        }
    }

    var mTackPicHelper: ImgSelectHelp? = null
    var UserImag: File? = null
    override fun initPresenter(): UserInfoPresenter = UserInfoPresenter(this)

    override fun layoutId(): Int = R.layout.act_user_info

    override fun initData() {
        mTackPicHelper = ImgSelectHelp(this)

        var userInfo = intent.getSerializableExtra("userInfo")
        userInfo?.let {
            userInfo as UserInfoBean
            sex_et.text = userInfo.sex
            phone_et.text = userInfo.account
            name_et.setText(if (userInfo.userName.isNullOrEmpty()) userInfo.account else userInfo.userName)
            ImageLoadHelp.loadImage(user_icon_iv, userInfo.headerUrl)
            name_et.setSelection(name_et.text.length)
        } ?: let {
            showToast("信息异常")
            finish()

        }

    }

    override fun initView() {
        sex_et.setOnClickListener {
            it as TextView
            val pictureSelectionStr: Array<String> = arrayOf("男", "女")
            val builder: ButtomDialog.Builder = ButtomDialog.Builder(this)
            builder.setContentData(pictureSelectionStr)
            builder.setListener(object : ButtomDialog.OnItemSelectListener {

                override fun OnItemSelected(dialog: Dialog, position: Int, title: String?) {
                    when (position) {
                        2 -> dialog.dismiss()
                        else -> {
                            it.text = title
                            dialog.dismiss()
                        }
                    }
                }
            })
            builder.Build().show()
        }
        user_icon_iv.setOnClickListener {
            showDialog()
        }
        submit_bt.setOnClickListener {
            UserImag?.let {
                presenter?.upload(it)
            } ?: let {
                val sex = if ("男".equals(sex_et.text.toString())) 1 else 2
                val userName = name_et.text.toString()
                presenter?.submitInfo(userName, null, sex)
            }

        }
    }

    override fun uploadSuccess(fileName: String) {
        val sex = if ("男".equals(sex_et.text.toString())) 1 else 2
        val userName = name_et.text.toString()
        presenter?.submitInfo(userName, fileName, sex)
    }

    override fun modifySuccess(success: String) {
        presenter?.getUserInfo()
    }

    override fun loadSuccess(userInfo: UserInfoBean) {
        Constants.userInfoBean = userInfo
        EventBus.getDefault().post(UserInfoEvent())
        showToast("修改成功")
        finish()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == CODE_GALLERY_REQUEST || requestCode == CODE_CAMERA_REQUEST || requestCode == CODE_RESULT_REQUEST
        ) {
            mTackPicHelper?.onActivityResult(requestCode, resultCode, data)
            return
        }
    }

    private fun showDialog() {

        //显示 获取图片对话框
        mTackPicHelper?.let {
            it.createDialog().show()
            it.setCropImage(true)
            it.setSelectPicCount(1)
            it.setPicCallback(object : ImgSelectHelp.PicCallback {
                override fun onFinish(list: List<String>) {
                    if (list == null || list.size == 0) return
                    val file = File(list[0])
                    ImageLoadHelp.loadImage(user_icon_iv, file)
                    UserImag = file
                }
            })
        }

    }

    override fun start() {
    }


}